package com.boardour.toupin;

import android.app.Activity;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;

import androidx.annotation.NonNull;

import com.boardour.wifip2p.WifiP2pCommon;
import com.boardour.wifip2p.popup.WifiP2pListPopup;
import com.boardour.wifip2p.receiver.ConnectManager;
import com.boardour.wifip2p.receiver.IConnectListener;

import cn.carhouse.permission.Permission;
import cn.carhouse.permission.XPermission;
import cn.carhouse.permission.callback.PermissionListenerAdapter;

/**
 * 1. 连接Wifi
 * 2. 连接成功后开始录屏
 */
public class WifiConnectUtils implements IConnectListener {
    private Activity mActivity;
    private ConnectManager connectManager;
    private DefLifecycleCallbacks callbacks = new DefLifecycleCallbacks() {
        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {
            if (mActivity == activity) {
                mActivity.getApplication().unregisterActivityLifecycleCallbacks(this);
                if (connectManager != null) {
                    connectManager.unregisterBroadcastReceiver(mActivity);
                    connectManager = null;
                }
            }
        }
    };
    private WifiP2pListPopup popup;

    public WifiConnectUtils(Activity activity) {
        this.mActivity = activity;
        activity.getApplication().registerActivityLifecycleCallbacks(callbacks);
        connectManager = new ConnectManager();
        connectManager.setConnectListener(this);
        connectManager.registerBroadcastReceiver(activity);
        requestConnectInfo();
    }

    private void requestConnectInfo() {
        WifiP2pCommon.getInstance().requestConnectionInfo(new WifiP2pManager.ConnectionInfoListener() {
            @Override
            public void onConnectionInfoAvailable(WifiP2pInfo info) {
                if (info == null || info.groupOwnerAddress == null) {
                    return;
                }

            }
        });
    }

    private OnWifiConnectListener onWifiConnectListener;

    public interface OnWifiConnectListener {
        void onWifiConnect(boolean isConnect);
    }


    public void showPop() {
        XPermission.with(mActivity)
                .permissions(Permission.LOCATION)
                .request(new PermissionListenerAdapter() {
                    @Override
                    public void onSucceed() {
                        realShowPop();
                    }
                });
    }

    private void realShowPop() {
        popup = new WifiP2pListPopup(mActivity, true);
        popup.show();
    }

    @Override
    public void onWifiP2pStateChanged(boolean wifiP2pEnable) {

    }

    @Override
    public void onPeersChanged() {

    }

    @Override
    public void onWifiP2pConnectionChanged(boolean wifiP2pConnect) {

    }

    @Override
    public void onWifiP2pDeviceChanged(WifiP2pDevice wifiP2pDevice) {
        if (wifiP2pDevice != null) {
            Log.e("TAG", "onWifiP2pDeviceChanged:" + (wifiP2pDevice.status == WifiP2pDevice.CONNECTED));
            boolean isConnect = wifiP2pDevice.status != WifiP2pDevice.AVAILABLE;
            if (WifiP2pDevice.AVAILABLE != wifiP2pDevice.status) {
                Log.e("TAG", "stopPeerDiscovery");
                WifiP2pCommon.getInstance().stopPeerDiscovery();
            } else {
                Log.e("TAG", "discoverPeers");
                WifiP2pCommon.getInstance().discoverPeers();
            }
            if (wifiP2pDevice.status == WifiP2pDevice.CONNECTED) {
                if (popup != null) {
                    popup.dismiss();
                }
                requestConnectInfo();
            }
            if (onWifiConnectListener != null) {
                onWifiConnectListener.onWifiConnect(wifiP2pDevice.status == WifiP2pDevice.CONNECTED);
            }
        }
    }

    public void setOnWifiConnectListener(OnWifiConnectListener onWifiConnectListener) {
        this.onWifiConnectListener = onWifiConnectListener;
    }
}
